//
// Created by ubuntu on 6/10/23.
//

#include <linux/limits.h>
#include "GPS_IPC.h"

sqlite3 *db;

void create_database() {
    int rc;
    char *err_msg = NULL;
    const char *sql = "CREATE TABLE "
                      "IF "
                      "NOT EXISTS "
                      "gps_signal "
                      "("
                      "    id         INTEGER  not null "
                      "        primary key autoincrement, "
                      "    time       char(20) default NULL, "
                      "    latitude   char(20) default NULL, "
                      "    NS         char(20) default NULL, "
                      "    longitude  char(20) default NULL, "
                      "    EW         char(20) default NULL, "
                      "    gps_status char(20) default NULL, "
                      "    wx_num     char(20) default NULL, "
                      "    HDOP       char(20) not null, "
                      "    HBGD       char(20) not null, "
                      "    height     char(20) default NULL, "
                      "    cf_time    char(20) default NULL, "
                      "    cf_ID      char(20) default NULL "
                      ");";

    rc = sqlite3_exec(db, sql, NULL, NULL, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", err_msg);
        sqlite3_free(err_msg);
        sqlite3_close(db);

    }

}

void insert(gpgga data) {
    char *err_msg = NULL;
    int rc;
    char *sql = "INSERT INTO gps_signal (time,latitude,NS,longitude,EW,gps_status,wx_num,HDOP,HBGD,height,cf_time,cf_ID) "
                "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')";
    char final_sql[521] = {'\0'};

    sprintf(final_sql, sql, data.time, data.latitude, data.NS, data.longitude, data.EW, data.gps_status, data.wx_num,
            data.HDOP, data.HBGD, data.height, data.cf_time, data.cf_ID);

    rc = sqlite3_exec(db, final_sql, NULL, NULL, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", err_msg);
        sqlite3_free(err_msg);
    }
}

void server() {

    int qid;
    message msg;
    char *err_msg = NULL;
    int rc = sqlite3_open("./gps_data.db", &db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n",
                sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }
    create_database();

    if ((qid = msgget(SVKEY, IPC_CREAT | 0666)) == -1) {
        perror("msgget");
        exit(1);
    }
    printf("open queue %d\n", qid);
    do {
        memset(msg.msg_text, 0, BUFFER_SIZE);
        if (msgrcv(qid, (void *) &msg, BUFFER_SIZE, 0, 0) < 0) {
            perror("msgrcv");
            exit(1);
        }
        //char *str = string_pretreatment(msg.msg_text);
        //strcpy(msg.msg_text, str);
        gpgga data = parse_strings(msg.msg_text);
        if (strcmp(data.gps_status, "2") != 0) {
            strcpy(data.cf_ID, "");
            strcpy(data.cf_time, "");
        }
        insert(data);
        printf("The message from process %ld : \n%s", msg.msg_type, toString(data));

    } while (strncmp(msg.msg_text, "quit", 4));
    if ((msgctl(qid, IPC_RMID, NULL)) < 0) {
        perror("msgctl");
        exit(1);
    }

    exit(0);
}

int main(int arg, char **args) {
    server();
    return 0;
}